import "libjv"
import './api/jv.js'
import router from './router/index.js'
import {createApp, h, nextTick, inject} from "vue";
import App from './App.vue'
import axios from 'axios'
import ElementPlus, {ElDialog, ElTable, ElTree} from 'element-plus'

ElDialog.props.closeOnClickModal.default = false
ElDialog.props.destroyOnClose.default = true
ElTable.props.treeProps.default = () => {
    return {children: "", hasChildren: "hasChildren"}
};

import './api/element.scss'
import ElementPlusExt from "element-plus-ext"
import toolBar from "./layout/tool-bar.vue"
import remark from "./component/remark.vue"
import "./api/settings.js"
import permission from "./api/permission.js"
import VueClipboard from 'vue-clipboard3'

window.lang = import.meta.env.VITE_APP_LANG


var env = import.meta.env;
window.BASE_URL = env.BASE_URL;
window.SERVER_HOST = env.VITE_APP_SERVER_HOST;
jv.APP_CODE = env.VITE_APP_APP_CODE;
jv.initEnvVars();

var app = createApp({
    render: () => h(App)
});


app.config.errorHandler = (err, instance, info) => {
    var msgs = [];
    try {
        msgs.push('Vue Error Handler caught:')
        msgs.push(`Error: ${err}`)          // 这就是原始的 Error 对象
        msgs.push(`Where: ${info}`)         // 错误发生的位置，如 "render"、"setup function"

        // 强烈建议：打印完整的堆栈
        msgs.push(`Stack: ${err.stack}`)

        console.error(msgs.join('\n'))
    } catch (e) {
        console.error(`处理错误时出错! ${e.message}`)
    }
    // console.error('Vue Error Handler caught:')
    // console.error('Error:', err)          // 这就是原始的 Error 对象
    // console.error('Component:', instance) // 出错的组件实例
    // console.error('Where:', info)         // 错误发生的位置，如 "render"、"setup function"
    //
    // // 强烈建议：打印完整的堆栈
    // console.error('Stack:', err.stack)
}

jv.initVue({axios: axios, router: router});


//权限
permission(jv);

jv.reset = function () {
};

jv.debugger = function (v) {
    console.log(v);
};


axios.interceptors.request.use(config => {

    var lang = window.lang;
    if (lang) {
        config.headers["lang"] = lang;
    }

    config.headers["iam"] = "no"
    return config;
}, error => {
    return Promise.reject(error);
});


axios.interceptors.response.use(resp => {
    return resp;
}, error => {
    var resp = error.response;
    if (resp) {
        var status = resp.status;
        if (status == 401) {
            jv.show_login();
            return Promise.reject(error);
        }
    }
    return Promise.reject(error);
});

jv.tabs_key = "$tabs"


app.use(router);


nextTick(it => {
    // jv.main.loadData();
    document.documentElement.classList.add("loaded");
});

import zhCn from 'element-plus/es/locale/lang/zh-cn'

app.use(VueClipboard);
app.use(ElementPlus, {
    locale: zhCn,
});
app.use(ElementPlusExt);


app.provide("jv", jv);
app.config.globalProperties.jv = jv;

app.use(() => {
    app.component("tool-bar", toolBar)
    app.component("remark", remark)
})

app.mount("#app");
